#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <unordered_map>
#include <algorithm>

struct ThemeData
{
    std::string theme_id;
    std::string market;
    std::string name;
    std::string plate_code;
    std::string parent_id;
    std::string level;
    std::string drive_logic;
    std::string create_time;
};

// 辅助函数：递归构建主题树
void buildThemeTree(const std::vector<ThemeData> &data, const std::string &parentId, std::map<std::string, std::vector<ThemeData>> &themeMap)
{
    for (const ThemeData &theme : data)
    {
        if (theme.parent_id == parentId)
        {
            themeMap[theme.parent_id].push_back(theme);
            buildThemeTree(data, theme.theme_id, themeMap);
            // 把递归出来的子题材也赋值给当前数组
            for (const ThemeData &element : themeMap[theme.theme_id])
                themeMap[theme.parent_id].push_back(element);
        }
    }
}

// 递归函数来构建树
void buildThemeTree2(const std::vector<ThemeData> &data, const std::string &parentId, std::map<std::string, std::vector<ThemeData>> &themeMap, std::vector<ThemeData> currentPath)
{
    for (const ThemeData &theme : data)
    {
        if (theme.parent_id == parentId)
        {
            // 记录题材追踪路径
            themeMap[theme.theme_id] = currentPath;
            // 添加当前主题到子题材列表
            themeMap[theme.theme_id].push_back(theme);
            // 递归构建子题材
            buildThemeTree2(data, theme.theme_id, themeMap, themeMap[theme.theme_id]);
        }
    }
}

int main()
{
    std::vector<ThemeData> data;
    // 添加表格中的全部数据（请确保data包含所有的数据）
    data.push_back({"9000000001", "1", "医药医疗", "910001", "0", "1", "医药医疗行业是我国国民经济的重要组成部分...", "20211201144516"});
    data.push_back({"9000000002", "1", "创新药", "910002", "0", "1", "创新药是指具有自主知识产权专利的药物...", "20211201144516"});
    data.push_back({"9000000003", "1", "连锁药店", "910003", "0", "1", "连锁药店是指在一个连锁总部统一管辖下...", "20211201144516"});
    data.push_back({"9000000004", "1", "生物疫苗", "910004", "9000000001", "2", "生物疫苗一般指生物技术疫苗...", "20211201144516"});
    data.push_back({"9000000006", "1", "流感疫苗", "910006", "9000000004", "3", "流感疫苗是指用来预防流行性感冒病毒引起的流行性感冒的疫苗...", "20211201144516"});
    data.push_back({"9000000007", "1", "宫颈癌疫苗", "910007", "9000000004", "3", "宫颈癌疫苗也称HPV疫苗...", "20211201144516"});
    data.push_back({"9000000008", "1", "狂犬病疫苗", "910008", "9000000004", "3", "狂犬病疫苗广泛使用的有Vero细胞纯化疫苗...", "20211201144516"});
    data.push_back({"9000000009", "1", "脑膜炎疫苗", "910009", "9000000004", "3", "脑膜炎疫苗就是流脑疫苗...", "20211201144516"});
    data.push_back({"9000000010", "1", "百白破疫苗", "910010", "9000000004", "3", "百白破疫苗是百日咳、白喉、破伤风混合疫苗的简称...", "20211201144516"});
    data.push_back({"9000000011", "1", "水痘疫苗", "910011", "9000000004", "3", "水痘疫苗是经水痘病毒传代毒株制备而成...", "20211201144516"});
    data.push_back({"9000000012", "1", "血制品", "910012", "9000000001", "2", "血制品是指各种人血浆蛋白制品...", "20211201144516"});
    data.push_back({"9000000013", "1", "医疗服务", "910013", "9000000001", "2", "医疗服务就是医院以病人和一定社会人群为主要服务对象...", "20211201144516"});
    data.push_back({"9000000014", "1", "眼科", "910014", "9000000013", "3", "眼科是研究发生在视觉系统...", "20211201144516"});
    data.push_back({"9000000015", "1", "口腔", "910015", "9000000013", "3", "口腔护理指对口腔内的牙、舌、腭、颊等部位进行清洁和保护的行为...", "20211201144516"});

    /*
    std::unordered_map<std::string, ThemeData> mThemeData2;
    for (auto it = data.begin(); it != data.end(); ++it)
    {
        mThemeData2.emplace(it->theme_id, *it);
    }
    for (auto it = data.begin(); it != data.end(); ++it)
    {
        //把父节点的themeId转化为plateCode编号
        auto pIt1 = mThemeData2.find(it->parent_id);
        if (pIt1 != mThemeData2.end())
        {
            it->parent_id = pIt1->second.plate_code;
        }
        //把节点的themeId转化为plateCode编号
        auto pIt2 = mThemeData2.find(it->theme_id);
        if (pIt2 != mThemeData2.end())
        {
            it->theme_id = pIt2->second.plate_code;
        }
    }
    */

    // 子到父题材
    {
        std::map<std::string, std::vector<ThemeData>> themeMap;
        // 调用递归函数，从根节点开始构建主题树
        buildThemeTree(data, "0", themeMap);
        // 打印主题树
        for (const auto &entry : themeMap)
        {
            std::cout << "Theme ID: " << entry.first << ", Children IDs: ";
            for (auto it = entry.second.begin(); it != entry.second.end(); ++it)
            {
                std::cout << it->theme_id << " -> ";
            }
            std::cout << std::endl;
        }
    }
    std::cout << "----------" << std::endl;
    {
        //父到子题材
        std::map<std::string, std::vector<ThemeData>> themeMap;
        // 调用递归函数，从根节点开始构建主题树
        buildThemeTree2(data, "0", themeMap, {});
        // 打印每个题材及其完整路径
        for (const auto &entry : themeMap)
        {
            std::cout << "Theme ID: " << entry.first << ", Parent IDs: ";
            for (auto it = entry.second.rbegin(); it != entry.second.rend(); ++it)
            {
                std::cout << it->parent_id << " -> ";
            }
            std::cout << std::endl;
        }
    }

    return 0;
}
